package com.ibeeking.found.file.rest.service.office.documentserver.managers.template;

import com.ibeeking.found.file.rest.service.office.documentserver.managers.document.DocumentManager;
import com.ibeeking.found.file.rest.service.office.documentserver.models.enums.DocumentType;
import com.ibeeking.found.file.rest.service.office.documentserver.models.filemodel.Template;
import com.ibeeking.found.file.rest.service.office.documentserver.storage.FileStoragePathBuilder;
import com.ibeeking.found.file.rest.service.office.documentserver.util.file.FileUtility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
@Qualifier("sample")
public class SampleTemplateManager implements TemplateManager {

    @Resource
    private DocumentManager documentManager;

    @Resource
    private FileStoragePathBuilder storagePathBuilder;

    @Resource
    private FileUtility fileUtility;

    // create a template document with the specified name
    public List<Template> createTemplates(String fileName){
        List<Template> templates = new ArrayList<>();
        templates.add(new Template("", "Blank", documentManager.getCreateUrl(fileName, false)));
        templates.add(new Template(getTemplateImageUrl(fileName), "With sample content", documentManager.getCreateUrl(fileName, true)));

        return templates;
    }

    // get the template image URL for the specified file
    public String getTemplateImageUrl(String fileName){
        DocumentType fileType = fileUtility.getDocumentType(fileName);  // get the file type
        String path = storagePathBuilder.getServerUrl(true);  // get server URL
        if(fileType.equals(DocumentType.word)){  // get URL to the template image for the word document type
            return path + "/css/img/file_docx.svg";
        } else if(fileType.equals(DocumentType.slide)){  // get URL to the template image for the slide document type
            return path + "/css/img/file_pptx.svg";
        } else if(fileType.equals(DocumentType.cell)){  // get URL to the template image for the cell document type
            return path + "/css/img/file_xlsx.svg";
        }
        return path + "/css/img/file_docx.svg";  // get URL to the template image for the default document type (word)
    }
}
